package cn.com.cym.gamezao.web;

import cn.com.cym.gamezao.annotation.AuthToken;
import cn.com.cym.gamezao.annotation.CommonApi;
import cn.com.cym.gamezao.base.SearchPage;
import cn.com.cym.gamezao.entity.Dict;
import cn.com.cym.gamezao.service.DictItemService;
import cn.com.cym.gamezao.service.DictService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Api(tags = "x-字典数据")
@RestController
@RequestMapping(value = "dict")
@CommonApi
public class DictController {

    final DictService thisService;

    final DictItemService dictItemService;

    public DictController(DictService dictService,DictItemService dictItemService){
        this.dictItemService=dictItemService;
        this.thisService=dictService;
    }

    @ApiImplicitParam(name = "code", value = "code", required = true,dataType = "String")
    @ApiOperation(value = "按照字典编码获取")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @GetMapping("/{code}")
    public ResponseEntity<?> getByCode(@PathVariable String code){
        Dict dict = this.thisService.getByCode(code);
        return ResponseEntity.ok(dict);
    }


    @ApiImplicitParam(name = "dict", value = "dict", required = true,dataType = "Dict")
    @ApiOperation(value = "新建字典")
    @ApiOperationSupport(order = 2, author = "mr.chen")
    @PostMapping()
    public ResponseEntity<?> add(@RequestBody Dict dict){
        final Dict storeDict = this.thisService.save(dict);
        return ResponseEntity.ok(storeDict);
    }


    @ApiImplicitParams({@ApiImplicitParam(name = "code", value = "code",paramType = "path", required = true,dataType = "String"),
            @ApiImplicitParam(name = "dict", value = "dict",paramType = "", required = true,dataType = "Dict")})
    @ApiOperation(value = "更新字典")
    @ApiOperationSupport(order = 3, author = "mr.chen")
    @PutMapping("/{code}")
    public ResponseEntity<?> update(@RequestBody Dict dict,@PathVariable String code){
        final Dict storeDict = this.thisService.updateByCode(dict,code);
        return ResponseEntity.ok(storeDict);
    }
    @ApiImplicitParam(name = "searchPage", value = "searchPage", required = true,dataType = "SearchPage")
    @ApiOperation(value = "分页显示page！")
    @ApiOperationSupport(order = 4, author = "mr.chen")
    @PostMapping("/page")
    @AuthToken
    public ResponseEntity<?> page(@RequestBody SearchPage searchPage){
        Page<?> byPageable = this.thisService.findByPageable(searchPage);
        return ResponseEntity.ok(byPageable);
    }


}
